package com.projecy.common.utils;

import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;


@Component
@RequiredArgsConstructor
public final class RedisUtil {

    private final RedisTemplate<String, Object> redisTemplate;

    public void setUrl(String key, Object value, Long time, TimeUnit timeUnit){
        System.out.println("保存URL");
        String valueUrL = value.toString();
        redisTemplate.opsForValue().set(key, valueUrL, time, timeUnit);
        System.out.println("保存URL==成功");
    }

    public String getUrl(String key){//获取值
        System.out.println("获取URL");
        Object object = redisTemplate.opsForValue().get(key);
        if (object == null){
            return null;
        } else {
            return object.toString();
        }
    }


    public void setValue(String key, Object value, Long time, TimeUnit timeUnit){
        System.out.println("保存值");
        String valueStr = value.toString();
        redisTemplate.opsForValue().set(key, valueStr, time, timeUnit);
        //redisTemplate.opsForValue().set(key, value, time, timeUnit);
        System.out.println("保存值==成功");
    }

    public String getValue(String key){//获取值
        System.out.println("获取值");
        Object object = redisTemplate.opsForValue().get(key);
        if (object == null){
            return null;
        } else {
            return object.toString();
        }
    }


    public void clearValue(String key){//删除对应key的值
        System.out.println("删除对应key的值");
        redisTemplate.delete(key);
    }

}
